<?php 
/**
 * 三国夫人后台
 */

class Module_kingdomslady{
	/**
	 * 结构函数
	 */
	function __construct(){
	
	}
	/**
	 * 三国夫人后台页面
	 */
	function index(){
		$smarty = loadSmarty();
		loadLib('App_kingdomslady');
		$list = App_kingdomslady::getAllMap();
		
		$smarty->assign('list' , $list);
		return $smarty->fetch('kingdomslady/index.tpl.php');
	}
	/**
	 * 三国夫人编辑地图
	 * 1、围墙 ，2、河流 ，3、绿洲 ，4、美女 ，5、武将 ，8、人物 ，9 ，通道
	 */
	function map(){
		$x = isset($_POST['x']) ? intval($_POST['x']) : null;
		$y = isset($_POST['y']) ? intval($_POST['y']) : null;
		$maps = isset($_POST['maps']) ? trim($_POST['maps']) : '';
		if(empty($x) || empty($y)) {
			return View::error('坐标不能为空或不能为字母或特殊符号！');
		}
		if (strlen($x) > 4 ||strlen($y) > 4){
			return View::error('坐标长度过长！');
		}
		if(empty($maps)) {
			return View::error('没有填写 地图名称');
		}
		$map_size = $x .'X'. $y;
		$x = $x + 1;
		$y = $y + 1;
		if($map_size != '6X6' && $map_size != '6X9'&& $map_size != '9X9' && $map_size != '9X16' && $map_size != '16X16' && $map_size != '16X32' && $map_size != '32X32' && $map_size != '32X48' && $map_size != '48X48' && $map_size != '64X64') {
			return View::error('地图坐标错误，现支持地图大小6X6,6X9,9X9,9X16,16X16,16X32,32X32,32X48,48X48,64X64');
		}
		$arr_x = array();
		$arr_y = array();
		for ($i = 0 ; $i <= $x ; $i++){
			$arr_x[] = $i;
			for ($j = 0 ; $j <= $y ; $j++){
				$arr_map[$i][$j] = 1;
			}
		}
		
		for ($j = 0 ; $j <= $y ; $j++){
			$arr_y[] = $j;
		}
		
		loadLib('App_kingdomslady');
		
		$uin=$_SESSION['adminid'];
		
		$code = App_kingdomslady::getAddMap($uin , $maps , $map_size , $arr_map);
		if ($code){
			return View::error('写入数据库失败');
		}
		$list = App_kingdomslady::getOneMapTime($uin);
		
		header('Location: ' . SITEURL . '/admin/?m=kingdomslady&a=mapalter&id='.$list['id']);
		
	}
	/**
	 * 坐标属性修改地图页面
	 */
	function mapadd(){
		$id = isset($_GET['id']) ? intval($_GET['id']) : null;
		$x = isset($_GET['x']) ? intval($_GET['x']) : null;
		$y = isset($_GET['y']) ? intval($_GET['y']) : null;
		if (empty($id) || empty($x) || empty($y)){
			return View::error('参数错误！');
		}
		loadLib('App_kingdomslady');
		$list = App_kingdomslady::getOneMap($id);
		if (empty($list)){
			return View::error('该数据不存在！');
		}
		$coord_x = count($list['map']) - 1;
		$coord_y = count($list['map'][0]) - 1;
		
		if ($y == 0 || $x == 0 || $y == $coord_y || $x == $coord_x){
			return View::error('这是一面无比坚硬的围墙，不可编辑');
		}
		$map = $list['map'][$x][$y];
		
		if (empty($map)){
			return View::error('该坐标不存在');
		}
		/******************攻击力最小为0，最大2000*******************/
		for ($k = 0; $k <= 20; $k++){
			$ap[] = $k * 100;
		}
		//print_r($ap);
		$smarty = loadSmarty();
		$smarty->assign('map' , $map);
		$smarty->assign('ap' , $ap);
		$smarty->assign('list' , $list);
		$smarty->assign('x' , $x);
		$smarty->assign('y' , $y);
	//	print_r($list['map'][$x][$y]);
		
		return $smarty->fetch('kingdomslady/addmap.tpl.php');
	}
	/**
	 * 显示地图页面
	 */
	function mapalter(){
		$id = isset($_GET['id']) ? intval($_GET['id']) : null;
		$paving = isset($_GET['paving']) ? intval($_GET['paving']) : null;
		if (empty($id)){
			return View::error('参数错误！');
		}
		loadLib('App_kingdomslady');
		$list = App_kingdomslady::getOneMap($id);
		if (empty($list)){
			return View::error('无此地图信息！');
		}
		//print_r($list);
		$x = count($list['map']) - 1;
		$y = count($list['map'][0]) - 1;
		$arr_x = array();
		$arr_y = array();
		for ($i = 0 ; $i <= $x ; $i++){
			$arr_x[] = $i;
		}
		for ($j = 0 ; $j <= $y ; $j++){
			$arr_y[] = $j;
		}
		$belle = App_kingdomslady::getOneBelle($list['belle_id']);
		
		$smarty = loadSmarty();
		
		$smarty->assign('paving' , $paving);
		$smarty->assign('coord_x' , $x);
		$smarty->assign('coord_y' , $y);
		$smarty->assign('arr_x' , $arr_x);
		$smarty->assign('arr_y' , $arr_y);
		$smarty->assign('list' , $list);
		$smarty->assign('belle' , $belle);
		//print_r($belle);
		return $smarty->fetch('kingdomslady/map.tpl.php');
	}
	/**
	 * 根据坐标来更改
	 */
	function mapschange(){
		$map = isset($_POST['map']) ? intval($_POST['map']) : null;
		$id = isset($_POST['id']) ? intval($_POST['id']) : null;
		$x = isset($_POST['x']) ? intval($_POST['x']) : null;
		$y = isset($_POST['x']) ? intval($_POST['y']) : null;
		
		loadLib('App_kingdomslady');
		if (empty($map) || empty($id) || empty($x) || empty($y)){
			return Format::json_return(1 , '参数错误');
		}
		$list = App_kingdomslady::getOneMap($id);
		
		if (empty($list)){
			return Format::json_return(1, '没有该地图信息');
		}
		$belle_id = $list['belle_id'];
		if ($map == 2 || $map == 3 || $map == 9 || $map == 8){
			$content = isset($_POST['content']) ? trim($_POST['content']) : '';
			$select = isset($_POST['select']) ? trim($_POST['select']) : '';
			if (empty($content)){
				return Format::json_return(1, '内容不能为空！');
			}
			
			/********************是否选择赏罚*********************/
			if (!empty($select)){
				$map_function = isset($_POST['map_function']) ? intval($_POST['map_function']) : null;
				$map_scene = isset($_POST['map_scene']) ? intval($_POST['map_scene']) : null;
				$function = isset($_POST['function']) ? intval($_POST['function']) : null;
				//return Format::json_return(1 , $map_function);
				if (empty($map_function) || empty($map_scene) || empty($function)){
					return Format::json_return(1 , '奖罚内容不能为空');
				}
				/********等有道具再开放*********/
				if ($map_function == 3){
						return Format::json_return(1 , '暂不支持道具');
				}
			}
		}
		
		
		if ($map == 4){
			$belle_id = isset($_POST['belle_id']) ? intval($_POST['belle_id']) : null;
			if (empty($belle_id)){
				return Format::json_return(1 , 'ID不能为空');
			}
			$belle = App_kingdomslady::getOneBelle($belle_id);
			if (empty($belle)){
				return Format::json_return(1 , '没有该美女信息');
			}
		}
		if ($map == 5){
			$general = isset($_POST['general']) ? intval($_POST['general']) : null;
			
			if (empty($general)){
				$ap_min = isset($_POST['ap_min']) ? intval($_POST['ap_min']) : null;
				$ap_max = isset($_POST['ap_max']) ? intval($_POST['ap_max']) : null;
				if ($ap_min == -1){
					return Format::json_return(1 , '没有选择MIN攻击武将!!!');
				}
				if ($ap_max == -1){
					return Format::json_return(1 , '没有选择MAX攻击武将!!!');
				}
				if ($ap_min >= $ap_max){
					return Format::json_return(1 , 'MIN攻击大于MAX攻击!!!');
				}
				$result = App_kingdomslady::getOneGeneralMinMax($ap_min, $ap_max);
				if (empty($result)){
					return Format::json_return(1 , $ap_min.'-'.$ap_max.'攻击武将不存在!!!');
				}
			}else{
				$general_id = isset($_POST['general_id']) ? intval($_POST['general_id']) : null;
				if (empty($general_id)){
					return Format::json_return(1 , '武将ID不能为空!!!');
				}
				$general = App_kingdomslady::getOneGeneral($general_id);
				if (empty($general)){
					return Format::json_return(1 , '没有该武将信息');
				}
			}
		}
		
		
	 	foreach ($list['map'] as $key => $value){
		   $arr[]= $value;
		 	if($x == $key){
		 		/************清空这个数组段的值************/
		 		$arr[$x][$y] = "";
		 		$arr[$x][$y] = array('map' => $map);
		    	if ($map == 2 || $map == 3 || $map == 9 || $map == 8){
		    		if (!empty($select)){
		    			$arr[$x][$y] = array('map' => $map , 'content' => $content , 'map_scene' => $map_scene , 'map_function' => $map_function , 'function' => $function);
		    		}else{
		    			$arr[$x][$y] = array('map' => $map , 'content' => $content);
		    		}
		    	}
		    	if ($map == 4){
		    		$arr[$x][$y] = array('map' => $map , 'belle_id' => $belle_id);
		    	}
		 		if ($map == 5){
		 			if (!empty($general)){
		 				$arr[$x][$y] = array('map' => $map , 'general_id' => $general_id);
		 			}else{
		    			$arr[$x][$y] = array('map' => $map , 'ap_min' => $ap_min , 'ap_max' => $ap_max);
		 			}
		    	}
		    }
		}
		
		$uin=$_SESSION['adminid'];
		$code = App_kingdomslady::getAlterMap($id , $uin , $arr , $list['name'] , $list['state'] , $belle_id);
		if ($code){
			return Format::json_return(1, '修改失败');
		}
		return Format::json_return(0 , '修改地图坐标('.$x.','.$y.')，成功！');/**/
	}
	function mapaltername(){
		$map_id = isset($_POST['map_id']) ? intval($_POST['map_id']) : null;
		$name = isset($_POST['maps']) ? trim($_POST['maps']) : '';
		$activate = isset($_POST['activate']) ? intval($_POST['activate']) : 2;
		//return Format::json_return(1, $activate);
		if (empty($map_id)){
			return Format::json_return(1, '参数错误');
		}
		if (empty($name)){
			return Format::json_return(1, '没有填写 地图名称');
		}
		loadLib('App_kingdomslady');
		$list = App_kingdomslady::getOneMap($map_id);
		if (empty($list)){
			return Format::json_return(1, '没有该地图信息');
		}
		$uin=$_SESSION['adminid'];
		
		$code = App_kingdomslady::getAlterMap($map_id , $uin , $list['map'] , $name , $activate , $list['belle_id']);
		if ($code){
			return Format::json_return(1, '修改失败');
		}
		return Format::json_return(0 , '修改地图名称'.$name.'成功');
	}
	/**
	 * 铺路模式
	 */
	function paving(){
		$id = isset($_GET['id']) ? intval($_GET['id']) : null;
		$x = isset($_GET['x']) ? intval($_GET['x']) : null;
		$y = isset($_GET['x']) ? intval($_GET['y']) : null;
		if (empty($id) || empty($x) || empty($y)){
			return View::error('参数错误！');
		}
		loadLib('App_kingdomslady');
		$list = App_kingdomslady::getOneMap($id);
		if (empty($list)){
			return View::error('没有该地图信息');
		}
		foreach ($list['map'] as $key => $value){
		   $arr[]= $value;
		 	if($x == $key){
		 		/************清空这个数组段的值************/
		 		$arr[$x][$y] = "";
		 		$arr[$x][$y] = array('map' => 9 , 'content' => '这是一条通道!!!');
		    }
		}
		$uin=$_SESSION['adminid'];
		
		
		$uin=$_SESSION['adminid'];
		$code = App_kingdomslady::getAlterMap($id , $uin , $arr , $list['name'] , $list['state'] , $list['belle_id']);
		if ($code){
			return View::error('修改失败!!!');
		}else{
			header('Location: ' . SITEURL . '/admin/?m=kingdomslady&a=mapalter&paving=2&id='.$list['id']);
		}
	}
	/**
	 * 删除地图
	 */
	function mapdelete() {
		$map_id = isset($_POST['map_id']) ? intval($_POST['map_id']) : null;

		if(empty($map_id)) {
			return Format::json_return(1 , '参数错误');
		}

		$Db = loadLib('Db');
		$Db->query("DELETE FROM `app_kingdoms_map` WHERE `id` = " . $map_id);

		return Format::json_return(0 , '删除成功');
	}
	
}
# end of script